package cn.sinjet.carassist;

import com.mgroup.wazehelper.R;
import com.waze.sdk.WazeSDKManager;

import android.R.integer;
import android.app.PendingIntent;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.os.Messenger;
import android.util.Log;
import cn.sinjet.viewmodel.ViewModel;

public class WazeClient {
	static final String tag = "waze";
    static WazeClient instance = null;
    int mDistance = -1;
    int mMyTurnIcon = -1;
    public static WazeClient getInstance(){
    	if(instance == null){
    		instance = new WazeClient();
    	}
    	return instance;
    }
    
    boolean mIsSDKInited = false;
    public void intClient(Context context,PendingIntent pendIntent){
    	 if(!mIsSDKInited){
    		 mIsSDKInited = WazeSDKManager.getInstance().initSDK(context,mRecvMessenger,pendIntent,-1,-1,null);
    		 Log.i(tag,"init waze sdk result:"+mIsSDKInited);
    	 }
    	 if(mIsSDKInited)  WazeSDKManager.getInstance().openWaze();
//    	 return mIsSDKInited;
    }
    
    
    
    Messenger mRecvMessenger = new Messenger(new MessageHandler());
    class MessageHandler extends Handler{
    	@Override
    	public void handleMessage(Message msg) {
    		// TODO Auto-generated method stub
    		super.handleMessage(msg);
    		int messageType = msg.what;
    		WazeSDKManager.Waze_Message message = WazeSDKManager.Waze_Message.values()[messageType];
			 Log.i(tag, "handle msg:"+message.toString());
			 switch(message){
			 case Waze_Message_CONNECTION_STATUS:
				  String ConnectedString = msg.getData().getString("STATUS");
				  boolean IsConnected = Boolean.valueOf(ConnectedString);
				  Log.i(tag, "connect state:"+IsConnected);
				  mIsSDKInited = IsConnected;
				  if(!IsConnected)  {
					  clearNavigating();
					  WazeSDKManager.getInstance().terminateSDK();
				  }
				  ViewModel.getIns().setViewProperty(R.id.waze_connect_state, IsConnected?1:0);
				 break;
			 case  Waze_Message_INSTRUCTION:{
				  String instruction = msg.getData().getString("INSTRUCTION");
				  int instructionCode = Integer.valueOf(instruction);
				  handleInstruction(instructionCode);
				  refreshTurnInfo();
    			}break;
			 case Waze_Message_DISTANCE:
				  String strDistance = msg.getData().getString("DISTANCE_METERS");
				  mDistance = Integer.valueOf(strDistance);
				  refreshTurnInfo();
				 break;
			 case Waze_Message_INSTRUCTION_EXIT:
				 clearNavigating();
				 break;
			 case Waze_Message_ROUTE:
				 break;
			 case Waze_Message_UNUSED:
				 break;
			 }
    	}
    }
    
    void clearNavigating(){
    	mDistance = -1;
    	mMyTurnIcon = -1;
    	SinjetApplication.getInstance().getMsgClient().onThirdPartyNavigating(false);
    }
    
    void handleInstruction(int instructionCode){
    	WazeSDKManager.Waze_Instructions_Types   types_message = WazeSDKManager.Waze_Instructions_Types.values()[instructionCode];
    	switch(types_message){
    	case  NAV_INSTR_NONE:
    		mMyTurnIcon = 1;
			break;
		case TURN_LEFT:
			mMyTurnIcon = 2;
			break;
		case TURN_RIGHT:
			mMyTurnIcon = 3;
			break;
		case  KEEP_LEFT:
			mMyTurnIcon = 4;
			break;
		case KEEP_RIGHT:
			mMyTurnIcon = 5;
			break;
	    case CONTINUE_STRAIGHT:
	    	mMyTurnIcon = 9;
           break;
	    case ROUNDABOUT_ENTER:
	    	mMyTurnIcon = 11;
	    	break;
	    case  ROUNDABOUT_EXIT:
	    	mMyTurnIcon = 12;
	    	break;
	    case ROUNDABOUT_LEFT:
	    	mMyTurnIcon = 18;
	    	break;
	    case ROUNDABOUT_RIGHT:
	    	mMyTurnIcon = 21;
	    	break;
	    case ROUNDABOUT_EXIT_LEFT:
	    	mMyTurnIcon = 18;
	    	break;
	    case ROUNDABOUT_EXIT_RIGHT:
	    	mMyTurnIcon = 21;
	    	break;
    	case APPROACHING_DESTINATION:
    		mMyTurnIcon = 15;
    		break;
    	case EXIT_LEFT:
    		mMyTurnIcon = 4;
    		break;
    	case EXIT_RIGHT:
    		mMyTurnIcon = 5;
    		break;
    	case WAYPOINT_DELAY:
    		mMyTurnIcon = 10;
    		break;
		case U_TURN://trun around
			mMyTurnIcon = 8;
			break;
		case NAV_INSTR_COUNT://what is it?
			mMyTurnIcon = 1;
			break;
    	}
    }
    
    void refreshTurnInfo(){
    	if(mMyTurnIcon == -1 || mDistance ==-1) return;
    	SinjetApplication.getInstance().getMsgClient().onThirdPartyNavigating(true);
    	SinjetApplication.getInstance().getMsgClient().onStepInfo(mMyTurnIcon, mDistance);
    	
   	     if(mMyTurnIcon == 15)
   		   SinjetApplication.getInstance().getMsgClient().onThirdPartyNavigating(false);
    }
}
